package best.driver.android.Activity.Order;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.Activity.Order.Adapter.AddressDetailAdapter;
import best.driver.android.AppInfo;
import best.driver.android.Beans.*;
import best.driver.android.BestApp;
import best.driver.android.R;
import best.driver.android.RequestFactory;
import best.driver.android.Utils.SizeUtils;
import best.driver.android.Utils.TimeUtil;
import best.driver.android.Utils.Util;
import best.driver.android.View.SelectPhoneDialog;
import best.driver.android.View.SelectThreeDialog;
import best.driver.android.common.app.AppProxyFactory;
import best.driver.android.common.network.ApiObjectRequest;
import best.driver.android.common.network.ApiRequest;
import best.driver.android.common.network.ApiStringRequest;
import best.driver.android.common.network.BestError;
import best.driver.android.manager.UserInfoManager;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import java.util.ArrayList;
import java.util.List;

public class OrderDetailActivity extends BaseActivity{

    OrderDetailInfo mOrderDetail;
    AddressDetailAdapter mAddressUnloadAdapter;
    AddressDetailAdapter mAddressLoadAdapter;

    @InjectView(R.id.tv_load_address)
    TextView mTvLoadAddress;

    @InjectView(R.id.tv_unload_address)
    TextView mTvUnloadAddress;

    @InjectView(R.id.tv_price_pre)
    TextView mTvPricePre;

    @InjectView(R.id.tv_price)
    TextView mTvPrice;

    @InjectView(R.id.tv_price_detail)
    TextView mTvPriceDetail;

    @InjectView(R.id.tv_pay_method)
    TextView mTvPayMethod;

    @InjectView(R.id.tv_extra)
    TextView mTvExtra;

    @InjectView(R.id.tv_remark)
    TextView mTvRemark;

    @InjectView(R.id.tv_time)
    TextView mTvTime;

    @InjectView(R.id.tv_order_id)
    TextView mTvOrderId;

    @InjectView(R.id.lv_multi_unload_address)
    ListView mLvMultiUnloadAddress;
    
    @InjectView(R.id.lv_multi_load_address)
    ListView mLvMultiLoadAddress;

    @OnClick(R.id.iv_phones)
    protected void showContacts() {
        if(mOrderDetail != null && mOrderDetail.orderAddresses != null && mOrderDetail.orderAddresses.size() > 0) {
            //SelectThreeDialog dialog = new SelectThreeDialog(OrderDetailActivity.this, mOrderDetail.orderAddresses);
            OrderAddresses address = new OrderAddresses();
            address.flag = "MAKER";
            address.phone = mOrderDetail.shipper.phone;
            List<OrderAddresses> addressesList = new ArrayList<OrderAddresses>();
            addressesList.addAll(mOrderDetail.orderAddresses);
            addressesList.add(address);
            SelectPhoneDialog dialog = new SelectPhoneDialog(OrderDetailActivity.this, addressesList);
        }
    }

    @OnClick(R.id.tv_price_detail)
    protected void showPriceDetail() {
        if(mOrderDetail != null) {
            Intent intent = new Intent(OrderDetailActivity.this, OrderFeeActivity.class);
            intent.putExtra("orderId", mOrderDetail.orderId);
            startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_order_detail);
        ButterKnife.inject(this, view);
        view.setVisibility(View.GONE);
        setTopTopic("订单详情");
        setLeftIvBak(R.drawable.icon_navibar_back);
        mAddressUnloadAdapter = new AddressDetailAdapter(this);
        mLvMultiUnloadAddress.setAdapter(mAddressUnloadAdapter);
        mAddressLoadAdapter = new AddressDetailAdapter(this);
        mLvMultiLoadAddress.setAdapter(mAddressLoadAdapter);
        getOrderDetail();
    }

    private void getOrderDetail() {
        showWaiting();
        String orderId = getIntent().getStringExtra("orderId");
        if(!TextUtils.isEmpty(orderId)) {
            ApiObjectRequest<OrderDetailInfo> request = RequestFactory.createGetOrderDetailInfoRequest(orderId);
            request.setListener(new ApiRequest.ApiRequestListener<OrderDetailInfo>() {
                @Override
                public void onRequestSuccess(OrderDetailInfo response) {
                    mOrderDetail = response;
                    view.setVisibility(View.VISIBLE);
                    hideWaiting();
                    if(response != null) {
                        updateOrderView(response);
                    }
                }

                @Override
                public void onRequestError(BestError error) {
                    hideWaiting();
                }
            });
            AppProxyFactory.getProxy().getNetworkManager().enqueueRequest(request);
        }
    }

    private void updateOrderView(OrderDetailInfo orderDetail) {
        //额外服务
//        StringBuilder builder = new StringBuilder();
//        if(orderDetail.orderExtraServices != null && orderDetail.orderExtraServices.size() > 0) {
//            List<OrderDetailInfo.OrderExtraServices> extraServices = orderDetail.orderExtraServices;
//            for(int i = 0; i < extraServices.size() - 1; i++) {
//                builder.append(extraServices.get(i).displayName).append(",");
//            }
//            builder.append(extraServices.get(extraServices.size()-1).displayName);
//        }
//        if(!TextUtils.isEmpty(builder.toString())) {
//            mTvExtra.setText(builder.toString());
//        }
        if(!TextUtils.isEmpty(orderDetail.extraServiceDetail)) {
            mTvExtra.setText(orderDetail.extraServiceDetail);
        }

        //订单时间
        mTvTime.setText(TimeUtil.convertToMDHM(orderDetail.bookTime));

        //货主备注
        if(!TextUtils.isEmpty(orderDetail.remark)) {
            mTvRemark.setText(orderDetail.remark);
        }

        //订单号
        if(!TextUtils.isEmpty(orderDetail.orderId)) {
            mTvOrderId.setText(orderDetail.orderId);
        }

        List<OrderAddresses> addresses = orderDetail.orderAddresses;
        List<OrderAddresses> unloadAddress = new ArrayList<OrderAddresses>();
        List<OrderAddresses> loadAddresses = new ArrayList<OrderAddresses>();
        if(addresses != null && addresses.size() > 0) {
            for(int i = 0; i < addresses.size(); i++) {
                OrderAddresses address = addresses.get(i);
                if(OrderStatus.SHIPPER_ADDRESS.equals(address.flag)) {
                    mTvLoadAddress.setText(address.address);
                    loadAddresses.add(address);
                } else if (OrderStatus.RECEIVER_ADDRESS.equals(address.flag)) {
                    mTvUnloadAddress.setText(address.address);
                    unloadAddress.add(address);
                }
            }
        }

        if(unloadAddress.size() > 0) {
            ViewGroup.LayoutParams params = mLvMultiUnloadAddress.getLayoutParams();
            float density = SizeUtils.getDensity(BestApp.getInstance().getApplicationContext());
            params.height = (int) (40 * density * unloadAddress.size());
            mLvMultiUnloadAddress.setLayoutParams(params);
            //mLvMultiUnloadAddress.setPadding(0, (int) (12 * density), 0, (int) (12 * density));
            mAddressUnloadAdapter.setData(unloadAddress);
        }
        
        if(loadAddresses.size() > 0) {
            ViewGroup.LayoutParams params = mLvMultiLoadAddress.getLayoutParams();
            float density = SizeUtils.getDensity(BestApp.getInstance().getApplicationContext());
            params.height = (int) (40 * density * loadAddresses.size());
            mLvMultiLoadAddress.setLayoutParams(params);
            //mLvMultiUnloadAddress.setPadding(0, (int) (12 * density), 0, (int) (12 * density));
            mAddressLoadAdapter.setData(loadAddresses);
        }

        if(OrderStatus.RETURNING.equals(orderDetail.status) || OrderStatus.FINISHED.equals(orderDetail.status)) {
            if(orderDetail.familiarPrice < 0.1) {
                mTvPrice.setText(Util.getOnePointNumber(orderDetail.actualPrice) + "元");
                mTvPriceDetail.setVisibility(View.VISIBLE);
            } else {
                mTvPrice.setText(Util.getOnePointNumber(orderDetail.familiarPrice) + "元");
                mTvPriceDetail.setVisibility(View.GONE);
            }
        } else {
            if(orderDetail.familiarPrice < 0.1) {
                mTvPricePre.setVisibility(View.VISIBLE);
                mTvPrice.setText(Util.getOnePointNumber(orderDetail.estimatePrice) + "元");
                mTvPriceDetail.setVisibility(View.GONE);
            } else {
                mTvPrice.setText(Util.getOnePointNumber(orderDetail.familiarPrice) + "元");
                mTvPriceDetail.setVisibility(View.GONE);
            }
        }

        if(!TextUtils.isEmpty(PayType.getInstance().getPayTypeMap().get(orderDetail.paymentMethod))) {
            mTvPayMethod.setText("("+PayType.getInstance().getPayTypeMap().get(orderDetail.paymentMethod)+")");
        }
    }
}
